(function($){
	$.fn.tol.multi= function(){
		if(typeof code == "string"){
			if(!options){
				options = {};
			}
			options = $.extend(options,{target:this});
			options = $.extend(options,$(options.target).data("options")||{});
			switch(code){
			case "reload":
				return reload(options);
			case "clear":
				return clear(options);
			case "disable":
				return disable(options);
			case "enable":
				return enable(options);
			case "reset":
				return reset(options);
			case "close":
				return close(options);
			case "parentText":
				return getParentText(options); 
			}
		}
		code = $.extend({}, $.fn.tol.multi.defaults, code || {});
		code = $.extend(code,{target:this});
		return init(code);
	}
	
	function init(options){
		var key = options.key;
		var showName = options.showName;
		options.backName = showName;
		var v = $("<div>", {
		  	style :"display:none",
		  	tol:"tol-multi-hide-div-"+key
		});
		v.appendTo("body");
		
		
		var html ='<div class="tol-multi-div">'+options.title.replace(new RegExp("\\{0\\}", "g"), showName)+'<a href="javascript:void(0);" class="tol-close" id="tol-close-'+key+'">&nbsp;</a></div>';
		html +='<div class="tol-multi-select">';
		
//		for(var i=0;i<options.urls.length-1;i++){
//			html +='<select id="tol-multi-hide-div-sel-'+key+'-'+i+'" ></select>';
//		}
		html +='<div class="tol-multi-back-up"><a href="javascript:void(0);" id="tol-multi-tree-a-back-up-'+key+'" >'+options.backUp+'</a></div>' +
				'<div class="tol-multi-tree-current-level" >'+options.currentLevel+':<font id="tol-multi-tree-current-show-'+key+'">'+options.rootName+'</font></div>';
				
		if(options.clear){
			html +='<div class="tol-multi-clear-btn" ><a id="tol-clear-func-'+key+'" href="javascript:void(0);">'+options.clearUp+'</a></div>';
			
			$('#tol-clear-func-'+key).live('click',function(){
				/*$.each($("input[id^='tol-multi-hide-div-item-checkbox-"+key+"-']"),function(i,data){
					$(data).attr('checked','');
				});*/
				
				clear(options);
				return options.clear(options);
			});
		}
		
		if(options.canRoot){
			html +='<div class="tol-multi-canroot-btn" ><a id="tol-clear-canroot-'+key+'" href="javascript:void(0);">'+options.canRoot.show+'</a></div>';
			
			$('#tol-clear-canroot-'+key).live('click',function(){
				
				options.canRoot.success(options);
				close(options);
			});
		}
		
		$('#tol-multi-tree-a-back-up-'+key).live('click',function(){
			return backUp(options);
	   	}); 
		
		html +='</div>';
		html +=
					'<input type="hidden" id="tol-multi-hide-div-first-'+key+'" value="true" />'+
					'<div class="tol-multi-item" id="tol-multi-hide-div-items-'+key+'"></div>'+
					'<div class="tol-clear" ></div>';
		
		var applyDiv;
		if(options.require){
			applyDiv = '<div>'+options.notFind+'<a href="javascript:tolMultiApply(\''+options.url+'\')">'+options.clickRequire+'</a></div>';
		}else{
			applyDiv = '';
		}
		if(options.multi){
			html+='<div class="tol-multi-separate" >'+
						applyDiv+
						'<div >'+options.yourCurrentChoose.replace(new RegExp("\\{0\\}", "g"), options.maxCount)+'</div>'+
						'<div class="tol-multi-choose" id="tol-multi-hide-div-my-choose-'+key+'">'+
						'</div>'+
						'<a id="tol-multi-hide-add-btn-'+key+'" class="tol-multi-add-btn" href="javascript:void(0);" title="'+options.confirm+'" ><span>'+options.confirm+'</span></a>'+
						'</div>';
		}else{
			html +='<div class="tol-multi-separate" >' +
						applyDiv+
						'<a id="tol-multi-hide-add-btn-'+key+'" class="tol-multi-add-btn hidden" href="javascript:void(0);" title="'+options.confirm+'" ><span>'+options.confirm+'</span></a>'+
						'</div>';
		}
//		$(document.body).append(html);
		$(v).append(html);
		
		if(options.multi){
			$('#tol-multi-hide-add-btn-'+key).click(function(){//完成添加后方法
				success(options);
			});
		}
		
		$('#tol-close-'+key).click(function(){
			return close(options);
		});
		
		var ids=[],texts=[];
		if(options.init!=null){
			options.init(ids,texts);
		}
		options = $.extend(options,{ids:ids,texts:texts});
		load(options);
		
		//绑定点击后弹出层事件
		$(options.target).bind('click',function(){
			return click(options);
	   	}); 
		
		$(options.target).data("options",options);
		if(options.pagination){
			$('#tol-multi-pagination-next-'+options.key).live('click',{options:options}, function(event) {
					toNext(event.data.options);
			});
			$('#tol-multi-pagination-prev-'+options.key).live('click',{options:options}, function(event) {
					toPrev(event.data.options);
			});
		}
	}
})(jQuery);

if($.fn.TOLMultiTree){
	$.fn.tol.multi.defaults.select = '选择';
	$.fn.tol.multi.defaults.yourCurrentChoose = '您目前选择的有(点击删除,最多选择{0}个)：';
	$.fn.tol.multi.defaults.confirm = '确定';
	$.fn.tol.multi.defaults.maxChoose = '您最多选择{0}个{1}';
	$.fn.tol.multi.defaults.haveChoosed = '您已经选择过该{0}';
	$.fn.tol.multi.defaults.close= '关闭';
	$.fn.tol.multi.defaults.lastLevel= '已经是最后一级了';
	$.fn.tol.multi.defaults.title= '请选择{0}(点击名称选择下一级)';
	$.fn.tol.multi.defaults.backUp= '返回上一层';
	$.fn.tol.multi.defaults.currentLevel= '当前层级';
	$.fn.tol.multi.defaults.clearUp= '清空';
	$.fn.tol.multi.defaults.notFind= '没找到？';
	$.fn.tol.multi.defaults.clickRequire= '点击申请';
}